﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using Ext.Net;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;


namespace TCDC_System.Content.MasterData
{
    public partial class p_tcdc_mst_information : SystemPage.UIPage
    {
        private tcdc_mst_information objInformation;
        protected void Page_Init(object sender, EventArgs e)
        {
            this.InformationAdd1.GridStore = StoreInformation;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            objInformation = new tcdc_mst_information();
            objInformation.strConn = ConfigurationManager.AppSettings["ConnString"];
            if (!X.IsAjaxRequest)
            {
                StoreInformation.DataSource = GetInformation();
                StoreInformation.DataBind();
            }
        }

        protected DataTable GetInformation()
        {
            DataTable dt = objInformation.getByObject(new tcdc_mst_information());
            return dt;
        }

        protected void AddInformation(object sender, DirectEventArgs e)
        {
            InformationAdd1.Show();
        }

        protected void UpdateInformation(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["InformationValue"];
            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string information_id = string.Empty;
            string information_group_id = string.Empty;
            string information_desc = string.Empty;
            string sorting_information_no = string.Empty;

            foreach (Dictionary<string, string> row in workingdays)
            {
                information_id = row["information_id"].ToString();
                information_group_id = row["information_group_id"].ToString();
                information_desc = row["information_desc"].ToString();
                sorting_information_no = row["sorting_information_no"].ToString();
            }

            objInformation.information_id = Convert.ToInt32(information_id);
            objInformation.information_group_id = Convert.ToInt32(information_group_id);
            objInformation.information_desc = information_desc;
            objInformation.sorting_information_no = Convert.ToInt32(sorting_information_no);
            objInformation.updateData(objInformation);
            StoreInformation.DataSource = GetInformation();
            StoreInformation.DataBind();
        }

        protected void DeleteInformation(object sender, DirectEventArgs e)
        {
            this.ConfirmDeleteInformation();
        }

        protected void StoreInformation_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StoreInformation.DataSource = GetInformation();
            StoreInformation.DataBind();
        }

        protected void ExportExcelInformation(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=Information.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
                DataGrid dataExportExcel = new DataGrid();
                dataExportExcel.DataSource = dt;
                dataExportExcel.DataBind();
                dataExportExcel.RenderControl(htmlWrite);
                StringBuilder sbResponseString = new StringBuilder();
                sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> <body>");
                sbResponseString.Append(stringWriter + "</body></html>");
                sbResponseString.Replace("&nbsp;", "&#160;");
                Response.Write(sbResponseString.ToString());
                Response.End();
            }
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeleteInformation()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataInformation()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataInformation()
        {
            RowSelectionModel sm = this.GridPanelInformation.SelectionModel.Primary as RowSelectionModel;
            string information_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                information_id = row.RecordID;
            }
            objInformation.deleteData(information_id);
            StoreInformation.DataSource = GetInformation();
            StoreInformation.DataBind();
        }
    }
}

